import 'package:baby_health_tracker/screen/onboarding_screen.dart';
import 'package:baby_health_tracker/screen/records_screen.dart';
import 'package:baby_health_tracker/screen/reminders_screen.dart';
import 'package:baby_health_tracker/service/notification_service.dart';
import 'package:baby_health_tracker/service/onboarding_service.dart';
import 'package:baby_health_tracker/service/settings_service.dart';
import 'package:baby_health_tracker/service/theme_notifier.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'theme/app_theme.dart';
import 'screen/home.dart';
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await ThemeNotifier.instance.init();
  final settings = await SettingsService.getInstance();
  await initializeDateFormatting();
  runApp(MyApp(settings: settings,));
}

@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse response) {
  debugPrint('Notification tapped in background: ${response.payload}');
}

class MyApp extends StatefulWidget {
  final SettingsService settings;

  const MyApp({Key? key, required this.settings})
    : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isDarkMode = false;

  @override
  void initState() {
    super.initState();
    _loadSettings();
    _listenToSettingsChanges();
  }

  void _loadSettings() {
    setState(() {
      _isDarkMode = widget.settings.isDarkMode;
    });
  }

  void _listenToSettingsChanges() {
    widget.settings.onSettingsChanged.listen((_) {
      _loadSettings();
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '宝宝健康追踪',
      initialRoute: '/home',
      theme: ThemeData(
        primaryColor: Colors.pink
      ),
      routes: {
        '/home': (context) => MainPage(),
        '/onboarding': (context) => const OnboardingScreen(),
        '/records': (context) => const RecordsScreen(),
        '/reminders':
            (context) => RemindersScreen(
              babyId: ModalRoute.of(context)!.settings.arguments as String,
            ),
      },
      debugShowCheckedModeBanner: false,
      builder: (context, child) {
        return MediaQuery(
          data: MediaQuery.of(
            context,
          ).copyWith(textScaler: TextScaler.linear(1.0)),
          child: child!,
        );
      },
    );
  }
}

